﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SIPO.master" AutoEventWireup="false" CodeFile="recDemandlist.aspx.cs" Inherits="SIPO.Web.Lists.Transaction.recDemandlist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>

    <div>
        <button id="btnAdd" type="button" onclick="OpenForm('add');">
            Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">
            Ubah</button>
    </div>
     <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog1" title="Default dialog">
    </div>
    <script type="text/javascript">
        $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
        $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
    </script>

     <script type="text/javascript">
         var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recDemandhandler.ashx") %>';

         $('#table').jqGrid({
             url: handlerUrl + '?method=getdata',
             datatype: 'json',
             colNames: ['id', 'No.', 'Nama Perusahaan', 'Jenis Pungutan', 'Jumlah','Tgl. Jatuh Tempo Tagihan','Nomor Surat','Tgl. Surat','Jenis Upaya Penagihan','Jatuh Tempo Upaya Penagihan','Lampiran'],
             colModel: [
           		{ name: 'DemandNo', key: true, hidden: true, index: 'DemandNo', width: 5 },
                { name: 'row_number', index: 'no', width: 30, align: 'center', search: false, searchoptions: { sopt: ['eq', 'cn']} },
           		{ name: 'DebtorName', index: 'recDebtor.DebtorName', width: 150, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'recDemandDetail.TransName', index: 'recDemandDetail.TransName', width: 85, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'recDemandDetail.TransValue', index: 'recDemandDetail.TransValue', width: 85, formatter: 'currency', searchtype: 'number', formatoptions: { prefix: '', suffix: '', thousandsSeparator: ',' }, align: 'right' },
                { name: 'recDemandDetail.DueDate', index: 'recDemandDetail.DueDate', align: 'center', width: 100, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                { name: 'recDemandDetail.LatterNo', index: 'recDemandDetail.LatterNo', width: 150, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'recDemandDetail.LatterDate', index: 'recDemandDetail.LatterDate', align: 'center', width: 100, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                { name: 'LastAction', index: 'recDemand.LastAction', width: 150, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'recDemandDetail.DueDateLetter', index: 'recDemandDetail.DueDateLetter', align: 'center', width: 165, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                { name: 'DemandNo', index: 'recDemandDetail.DemandNo', align: 'center', width: 100, search: false, formatter: returnMyLink },

           	],
             rowNum: 10,
             id: 'DemandNo',
             multiselect: true,
             rowList: [5, 10, 15, 20],
             pager: '#pager',
             sortname: 'recDemandDetail.DemandDetailNo',
             sortorder: "asc",
             viewrecords: true,
             jsonReader: { repeatitems: false },
             height: '100%',
             caption: 'Upaya Penagihan',
             ajaxGridOptions: { cache: false },
             beforeSelectRow: function (rowid, e) {
                 $('#table').jqGrid('resetSelection');
                 return (true);
             },

             ondblClickRow: function () {
                    OpenForm('edit');
                },
                gridComplete: function () {
                    var ids = $('#table').jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var _id = $('#table').jqGrid('getCell', ids[i], 'DemandNo');
                        
                        idx = "<a href='javascript:void(0);' class='dialog_link ui-corner-all' title='Upaya Penagihan' onclick=\"onclickButtonDemand('" + _id + "');\"><span class='ui-icon ui-icon-newwin'></span>...</a>";

                        $('#table').jqGrid('setRowData', ids[i], { aksi: idx });
                    }
                }
            });

            function returnMyLink(cellValue, options, rowdata) {
                var handleurlDownload = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx?method=downloadExpense") %>';
                //return "<a target='_blank' href='" + handleurlDownload + "&sanction_id=" + cellValue + "' >download</a>";
                return "<a  href='#' onclick='downloadfile(" + cellValue + ")' >download</a>";
            }
            function downloadfile(cellValue) {
                var obj = new Object();
                obj.DemandNo = encodeURIComponent(cellValue);
                jsonData = JSON.stringify(obj);

                var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx") %>';

                $.ajax({
                    type: 'POST',
                    url: handlerUrl + "?method=checkfiledemand",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: jsonData,
                    success: function (data) {
                        if (data.IsSuccess) {
                            var win = window.open(handlerUrl + '?method=downloaddemand&DemandNo=' + cellValue, '_blank');
                            win.focus();
                        }
                        else {
                            alert(data.Message);
                        }
                    },
                    error: function (response) {
                        alert('Error saat download file!');
                    }
                });

            }
 

         $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true, showQuery: true });

         function onclickButtonDemand(id) {
             $('#table').jqGrid('resetSelection');
             
             var urlList = '<%= ResolveUrl("~/forms/Transaction/recDemandform.aspx?aksi=insert&DemandNo=' + id + '") %>';
             $('#dialog1').load(urlList).dialog({ autoOpen: false, modal: false, width: 450, height: 300, title: 'Input Teguran' });
             $('#dialog1').dialog('open');
         }

         function OpenForm(ctl) {

             var formUrl = '<%= ResolveUrl("~/forms/transaction/recDemandform.aspx") %>';

             var grid = $('#table');
             var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
             $.each(selIds, function () {
                 selText.push(grid.jqGrid('getCell', this, 'DemandNo'));
             });
             if (ctl == 'edit') {
                 if (selIds.length == 0)
                     alert('Pilih data yang mau di edit!');
                 else {
                     if (selIds.length > 1) {
                         alert('Pilih satu record untuk ubah data!');
                     }
                     else if (selIds.length == 1) {
                         $('#dialog1').load(formUrl + '?aksi=update&DemandNo=' + selText).dialog({ autoOpen: false, modal: true, width: 450, title: 'Ubah Upaya Penagihan' });
                         $("#dialog1").dialog('open');
                     }
                 }
             }else
                 if (ctl == 'add') {
                     if (selIds.length == 0)
                         alert('Pilih data yang mau di tambah!');
                     else {
                         if (selIds.length > 1) {
                             alert('Pilih satu record untuk tambah data!');
                         }
                         else if (selIds.length == 1) {
                             $('#dialog1').load(formUrl + '?aksi=insert&DemandNo=' + selText).dialog({ autoOpen: false, modal: true, width: 450, title: 'Tambah Upaya Penagihan' });
                             $("#dialog1").dialog('open');
                         }
                     }
                 }
             }
     </script>
</asp:Content>

